// pages/apply/find/finddesc.js
const app = getApp()
var CONFIG = require("../../../config/config.js");
var share = require('../../../utils/share.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        sucfalse:false,
        class_id:'',
        ischangeaddress:false,
        address:'',
        is_class:1, //1普通期次查询 2班主任班级管理
        arrpeoplelist:[],
        isqi:1,  //1期次列表 2学员列表
        datestart: '',
        dateend: '',
        datestart1: '',
        dateend1: '',
        typearr: [],
        catearr: [{ id: 0, title: '全部' }, { id: 1, title: '技能' }, { id: 2, title: '理论' }, { id: 3, title: '全程' }],
        teamarr: [],
        phone: '',
        use_year1: '2024',
        use_year: '2024',
        team_acc_id: '', //校区id
        team_id: [''],
        is_type: '', //专业id
        is_type1: '', //专业id
        is_cate: '',
        is_cate1: 1,
        is_cate2: 1,
        is_cate3: '',
        showfalse: false,
        choosefalse: false,
        type: 1, //type 1技能提升 2执业医师技能 3执业医师理论
        arrlist: [],
        page: 0,
        keyword: '',
        is_cate_name1: '',
        is_cate_name: '',
        is_type_name: '',
        is_type_name1: '',
        team_acc_name: '',
        team_acc_name_arr: [],
        shufalse: false,
        roundarr: [], //轮次
        is_all_show: 1,
        is_team: 1,
        is_time: 1,
        is_round: 1,
        is_status: 1,
        round_id: '',
        round_id_arr: [''],
        round_name: '',
        round_acc_name_arr: [],
        statusarr: [{ title: '全部', id: '' }, { title: '可报名', id: 2 }, { title: '不可报名', id: 4 }, { title: '已报满', id: 3 }, { title: '停用', id: 1 }],
        statusid: '',
        statusid1: '',
        statusname: '',
        pxarr: [{ title: '即将开课班级', id: 1 },{ title: '全部班级', id: 0 }],
        pxid: 1,
        pxid1: 1,
        pxname: '',
        is_px: 1,
        statusname1: '',
        pxname1: '',
        sucfalse1:false,
        issure:1,
        speclist:[],
        specindex:0,
        titleshowerfalse:false,
    },
    clickdec(e){
      var that = this;
      var item = e.currentTarget.dataset.item;
      wx.showLoading({
          title: '加载中~',
      })
      var objdata = {
        class_id: item.id, //	是	int	班级学员ID
      }
      share.wxRequest('crmapi/User/classSpec', objdata, function(res) {
          wx.hideLoading();
          if (Number(res.data.errcode) == 0) {
              that.setData({
                titleshowerfalse: true,
                specindex:0,
                speclist:res.data.data,
              })
          } else {
              wx.showToast({
                  title: res.data.msg,
                  icon: 'none',
              })
          }
      })
    },
    changetitleclick(e){
      var index = e.currentTarget.dataset.index || e.target.dataset.index;
      this.setData({
        specindex:index,
      })
    },
    closetitleclick(){
      this.setData({
        titleshowerfalse:false,
      })
    },
    noclick(){
        this.setData({
            ischangeaddress:false,
        })
    },
    noclicksuc(){
        this.setData({
            sucfalse:false,
            sucfalse1:false,
        })
    },
    // 点击确定通知
    submitsuc(){
        var that = this;
        if(Number(that.data.issure)==1){
            that.setData({
                issure:2,
            })
            wx.showLoading({
                title: '加载中~',
            })
            var objdata = {
                class_id: that.data.class_id, //	是	int	班级学员ID
            }
            share.wxRequest('crmapi/user/classSms', objdata, function(res) {
                wx.hideLoading();
                if (Number(res.data.errcode) == 0) {
                    wx.showToast({
                        title: '发送成功',
                        icon: 'none',
                    })
                    that.setData({
                        sucfalse: false,
                        issure:1,
                        sucfalse1:false,
                    })
                } else {
                    that.setData({
                        issure:1,
                    })
                    wx.showToast({
                        title: res.data.msg,
                        icon: 'none',
                    })
                }
            })
        }
    },
    pltz(e){
        var that = this;
        var item = e.currentTarget.dataset.item || e.target.dataset.item;
        that.setData({
            class_id:item.id,
            sucfalse1:true,
        })
    },
    changeclas(e){
        var that = this;
        var item = e.currentTarget.dataset.item || e.target.dataset.item;
        if(item.address){
            var address = item.address;
        }else{
            var address = '';
        }
        that.setData({
            class_id:item.id,
            address:address,
            ischangeaddress:true,
        })
    },
    // 点击确定
    bindFormSubmit(e){
        var that = this;
        var value = e.detail.value.textarea;
        if (value == '') {
            wx.showToast({
                title: '请填写上课地址',
                icon: 'none',
                duration: 1500
            })
        } else {
            wx.showLoading({
                title: '加载中~',
            })
            var objdata = {
                class_id: that.data.class_id, //	是	int	班级学员ID
                address: value, //	否	int	备注
            }
            share.wxRequest('crmapi/user/classAddress', objdata, function(res) {
                if (Number(res.data.errcode) == 0) {
                    wx.showToast({
                        title: '修改成功',
                        icon: 'none',
                        duration: 1500
                    })
                    that.setData({
                        ischangeaddress: false,
                        sucfalse:true
                    })
                    that.listpeople();
                } else {
                    wx.showToast({
                        title: res.data.msg,
                        icon: 'none',
                        duration: 1500
                    })
                    wx.hideLoading();
                }
            })
        }
    },
    allstudentsclick(e){
        var that = this;
        var item = e.currentTarget.dataset.item || e.target.dataset.item;
        wx.navigateTo({
          url: '/pages/apply/find/newstudents/allstudents?class_id='+item.id+'&title='+item.title+'&type='+item.type+'',
        //   url: '/pages/apply/find/newstudents/allstudents?class_id='+item.id+'&title='+item.title+'&type='+item.type+'&date_start='+item.date_start+'&date_end='+item.date_end+'&round_name='+item.round_name+'',
        })
    },
    studentdecclick(e){
        var that = this;
        var item = e.currentTarget.dataset.item || e.target.dataset.item;
        wx.navigateTo({
          url: '/pages/apply/find/newstudents/studentdec?student_id='+item.student_id+'',
        })
    },
    zyzlclick(e) {
        var that = this;
        var type = e.currentTarget.dataset.type;
        that.setData({
            type: type,
            is_type1: '',
            is_type: '',
            is_cate3: '',
            is_cate: '',
            is_cate_name: '',
            is_type_name: '',
            is_cate_name1: '',
            is_type_name1: '',
            page:0
        })
        if (Number(type) == 3) {
            that.jntsCate();
        }
        that.listpeople();
    },
    showernon() {
        this.setData({
            showfalse: false,
        })
    },
    closech() {
        var that = this;
        if(Number(that.data.is_class)==2){
            var pxname = '按课程上课时间排序';
            var pxname1 = '按课程上课时间排序';
        }else{
            var pxname = '即将开课班级';
            var pxname1 = '即将开课班级';
        }
        that.setData({
            statusname1: '',
            pxname1: pxname1,
            type: 5,
            pxid:1,
            pxid1:1,
            pxname: pxname,
            statusid: '',
            statusid1: '',
            statusname: '',
            round_id: '',
            round_name: '',
            round_id_arr: [''],
            round_acc_name_arr: [],
            is_px: 1,
            is_cate1: 1,
            is_cate2: 1,
            is_team: 1,
            is_time: 1,
            is_round: 1,
            is_status: 1,
            is_all_show: 1,
            showfalse: false,
            choosefalse: false,
            team_acc_name: '',
            team_acc_name_arr: [],
            page: 0,
            datestart: '',
            dateend: '',
            datestart1: '',
            dateend1: '',
            use_year1: '2024',
            use_year: '2024',
            team_acc_id: '', //校区id
            team_id: [''],
            city_acc_id: '', //校区id
            is_type: '', //专业id
            is_type1: '', //专业id
            is_cate: '', //专业id
            is_cate3: '', //专业id
            is_cate_name1: '',
            is_cate_name: '',
            is_type_name: '',
            is_type_name1: '',
        })
        this.listpeople();
    },
    closeclcikll() {
        var that = this;
        that.setData({
            is_px: 1,
            is_cate1: 1,
            is_cate2: 1,
            is_team: 1,
            is_time: 1,
            is_round: 1,
            is_status: 1,
            is_all_show: 1,
        })
    },

    //  选择起始时间
    bindDateChangestart: function(e) {
        this.setData({
            datestart1: e.detail.value,
        })
    },

    // 结束时间
    bindDateChangeend: function(e) {
        if (this.data.datestart1 == '') {
            wx.showToast({
                title: '请先选择开始时间',
                icon: 'none',
                duration: 1500
            })
        } else {
            this.setData({
                dateend1: e.detail.value,
            })
        }
    },

    // 选择专业
    cateChange(e) {
        var that = this;
        var item = e.currentTarget.dataset.item || e.target.dataset.item;
        that.setData({
            is_cate3: item.id,
            is_cate_name1: item.title
        })
    },
    paymentChange(e) {
        var that = this;
        var item = e.currentTarget.dataset.item || e.target.dataset.item;
        that.setData({
            is_type1: item.id,
            is_type_name1: item.title
        })
    },

    // 选择月份
    changeyear(e) {
        var that = this;
        that.setData({
            use_year1: e.detail.value
        })
    },

    statusChange(e) {
        var that = this;
        var item = e.currentTarget.dataset.item || e.target.dataset.item;
        that.setData({
            statusname1: item.title,
            statusid1: item.id
        })
    },
    pxChange(e) {
        var that = this;
        var item = e.currentTarget.dataset.item || e.target.dataset.item;
        that.setData({
            pxname1: item.title,
            pxid1: item.id
        })
    },
    // 选择校区
    teamChange(e) {
        var that = this;
        var item = e.currentTarget.dataset.item || e.target.dataset.item;
        if (item.id == '') {
            var team_id = [''];
            var team_acc_name_arr = ['全部'];
        } else {
            var team_id = that.data.team_id;
            var team_acc_name_arr = that.data.team_acc_name_arr;
            if (team_id.indexOf(item.id) >= 0) {
                var index = team_id.indexOf(item.id);
                var index1 = team_acc_name_arr.indexOf(item.title);
                team_id.splice(index, 1);
                team_acc_name_arr.splice(index1, 1);
            } else {
                team_id.push(item.id);
                team_acc_name_arr.push(item.title);
            }
            if (team_id[0] == '') {
                team_id.splice(0, 1);
            }
        }
        that.setData({
            team_id: team_id,
            team_acc_name_arr: team_acc_name_arr,
        })
    },
    roundChange(e) {
        var that = this;
        var item = e.currentTarget.dataset.item || e.target.dataset.item;
        if (item.id == '') {
            var round_id_arr = [''];
            var round_acc_name_arr = ['全部'];
        } else {
            var round_id_arr = that.data.round_id_arr;
            var round_acc_name_arr = that.data.round_acc_name_arr;
            if (round_id_arr.indexOf(item.id) >= 0) {
                var index = round_id_arr.indexOf(item.id);
                var index1 = round_acc_name_arr.indexOf(item.name);
                round_id_arr.splice(index, 1);
                round_acc_name_arr.splice(index1, 1);
            } else {
                round_id_arr.push(item.id);
                round_acc_name_arr.push(item.name);
            }
            if (round_id_arr[0] == '') {
                round_id_arr.splice(0, 1);
            }
            if (round_acc_name_arr[0] == '全部') {
                round_acc_name_arr.splice(0, 1);
            }
        }
        that.setData({
            round_id_arr: round_id_arr,
            round_acc_name_arr: round_acc_name_arr,
        })
    },
    cateclick() {
        var that = this;
        var is_team = that.data.is_team;
        if (Number(is_team) == 1) {
            var is_team1 = 2;
            var is_all_show = 2;
        } else {
            var is_team1 = 1;
            var is_all_show = 1;
        }
        that.setData({
            is_cate1: is_team1,
            is_cate2: 1,
            is_team: 1,
            is_all_show: is_all_show,
            is_time: 1,
            is_round: 1,
            is_status: 1,
            is_px: 1,
        })
    },
    zyclick() {
        var that = this;
        var is_team = that.data.is_team;
        if (Number(is_team) == 1) {
            var is_team1 = 2;
            var is_all_show = 2;
        } else {
            var is_team1 = 1;
            var is_all_show = 1;
        }
        that.setData({
            is_cate1: 1,
            is_cate2: is_team1,
            is_team: 1,
            is_all_show: is_all_show,
            is_time: 1,
            is_round: 1,
            is_status: 1,
            is_px: 1,
        })
    },
    // /点击校区
    teamclick() {
        var that = this;
        var is_team = that.data.is_team;
        if (Number(is_team) == 1) {
            var is_team1 = 2;
            var is_all_show = 2;
        } else {
            var is_team1 = 1;
            var is_all_show = 1;
        }
        that.setData({
            is_team: is_team1,
            is_all_show: is_all_show,
            is_time: 1,
            is_round: 1,
            is_status: 1,
            is_px: 1,
            is_cate1: 1,
            is_cate2: 1,
        })
    },
    // /点击校区
    timeclick() {
        var that = this;
        var is_team = that.data.is_time;
        if (Number(is_team) == 1) {
            var is_team1 = 2;
            var is_all_show = 2;
        } else {
            var is_team1 = 1;
            var is_all_show = 1;
        }
        that.setData({
            is_time: is_team1,
            is_team: 1,
            is_round: 1,
            is_status: 1,
            is_px: 1,
            is_all_show: is_all_show,
            is_cate1: 1,
            is_cate2: 1,
        })
    },

    roundclick() {
        var that = this;
        var is_team = that.data.is_round;
        if (Number(is_team) == 1) {
            var is_team1 = 2;
            var is_all_show = 2;
        } else {
            var is_team1 = 1;
            var is_all_show = 1;
        }
        that.setData({
            is_round: is_team1,
            is_team: 1,
            is_time: 1,
            is_status: 1,
            is_px: 1,
            is_all_show: is_all_show,
            is_cate1: 1,
            is_cate2: 1,
        })
    },

    statusclick() {
        var that = this;
        var is_team = that.data.is_status;
        if (Number(is_team) == 1) {
            var is_team1 = 2;
            var is_all_show = 2;
        } else {
            var is_team1 = 1;
            var is_all_show = 1;
        }
        that.setData({
            is_status: is_team1,
            is_team: 1,
            is_round: 1,
            is_time: 1,
            is_px: 1,
            is_all_show: is_all_show,
            is_cate1: 1,
            is_cate2: 1,
        })
    },

    pxclick() {
        var that = this;
        var is_team = that.data.is_px;
        if (Number(is_team) == 1) {
            var is_team1 = 2;
            var is_all_show = 2;
        } else {
            var is_team1 = 1;
            var is_all_show = 1;
        }
        that.setData({
            is_status: 1,
            is_team: 1,
            is_round: 1,
            is_time: 1,
            is_px: is_team1,
            is_all_show: is_all_show,
            is_cate1: 1,
            is_cate2: 1,
        })
    },

    searchclick(e) {
        var that = this;
        // keykown
        var keyword = e.detail.value.keykown;
        that.setData({
            keyword: keyword,
            page: 0,
        })
        if(Number(that.data.is_class)==2){
            that.listpeople();
            that.setData({
                isqi:1,
            })
        }else{
            var tel = keyword.replace(/\s+/g, "");
            if (!(/^1\d{10}$/.test(tel))) {
                that.listpeople();
                that.setData({
                    isqi:1,
                })
            }else{
                that.setData({
                    isqi:2,
                })
                that.consStuList();
            }
        }
    },
    sureclick() {
        var that = this;
        if (Number(that.data.pxid1) == 2) {
            var picsx = '';
        } else {
            var picsx = that.data.pxid1;
        }
        that.setData({
            team_acc_id: that.data.team_id.join(','),
            team_acc_name: that.data.team_acc_name_arr.join(','),
            round_id: that.data.round_id_arr.join(','),
            round_name: that.data.round_acc_name_arr.join(','),
            statusname: that.data.statusname1,
            pxname: that.data.pxname1,
            statusid: that.data.statusid1,
            pxid: picsx,
            is_px: 1,
            is_team: 1,
            is_time: 1,
            is_round: 1,
            is_status: 1,
            is_all_show: 1,
            is_cate2: 1,
            is_cate1: 1,
            use_year: that.data.use_year1,
            is_type: that.data.is_type1,
            is_type_name: that.data.is_type_name1,
            is_cate: that.data.is_cate3,
            is_cate_name: that.data.is_cate_name1,
            datestart: that.data.datestart1,
            dateend: that.data.dateend1,
            showfalse: false,
            choosefalse: true,
            page: 0,
        })
        this.listpeople();
    },


    // 获取校区
    teams() {
        var that = this;
        share.wxRequest('crmapi/User/team', {}, function(res) {
            if (Number(res.data.errcode) == 0) {
                var data = res.data.data;
                var obj = [{
                    id: '',
                    title: '全部',
                }]
                that.setData({
                    teamarr: obj.concat(data)
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this;
        var phone = wx.getStorageSync('tel');
        // var type = 5;
        var type = options.type;
        if(options.is_class){
            var is_class = options.is_class;
        }else{
            var is_class = 1;
        }
        that.setData({
            phone: phone,
            type: type,
            is_class:is_class,
        })
        that.teams();
        if(Number(is_class)==2){
            wx.setNavigationBarTitle({
                title: '班级管理',
            })
            that.setData({
                statusarr:[{ title: '全部', id: '' }, { title: '可报名', id: 2 }, { title: '不可报名', id: 4 }, { title: '已报满', id: 3 }],
                pxarr: [{ title: '按课程上课时间排序', id: 1 }, { title: '按创建时间排序', id: 2 }],
                pxname:'按课程上课时间排序',
                pxname1:'按课程上课时间排序',
            })
        }else{
            that.setData({
                pxname:'即将开课班级',
                pxname1:'即将开课班级',
            })
            
            if (Number(type) == 1) {
                wx.setNavigationBarTitle({
                    title: '执业医师',
                })
            } else if (Number(type) == 3) {
                wx.setNavigationBarTitle({
                    title: '技能提升',
                })
                that.jntsCate();
            } else if (Number(type) == 2) {
                wx.setNavigationBarTitle({
                    title: '主治',
                })
            } else if (Number(type) == 5) {
                wx.setNavigationBarTitle({
                    title: '期次查询',
                })
            }
        }
        that.listpeople();
        that.roundList();
    },

    // 轮次
    roundList() {
        var that = this;
        share.wxRequest('crmapi/User/roundList', {}, function(res) {
            if (Number(res.data.errcode) == 0) {
                var data = res.data.data.lists;
                var obj = [{
                    id: '',
                    name: '全部',
                }]
                that.setData({
                    roundarr: obj.concat(data)
                })
            }
        })
    },

    // typearr
    // 技能提升下的二级分类
    jntsCate() {
        var that = this;
        share.wxRequest('crmapi/user/jntsCate', {}, function(res) {
            if (Number(res.data.errcode) == 0) {
                var data = res.data.data.lists;
                var obj = [{
                    id: '',
                    title: '全部',
                }]
                that.setData({
                    typearr: obj.concat(data)
                })
            }
        })
    },

    listpeople() {
        var that = this;
        wx.showLoading({
            title: '加载中~',
        })
        var type = that.data.type;
        if (Number(type) == 5) {
            var type1 = '';
        } else {
            var type1 = type;
        }
        var obj = {
            type: type1,
            keyword: that.data.keyword,
            use_year: that.data.use_year, //	否	int	使用年份
            cate_id: that.data.is_type, //	否	int	类型 
            is_type: that.data.is_cate, //	0-全部1-技能 2-理论 3-全程 （type等于1-执业医师 才有该参数）
            area_id: that.data.team_acc_id, //	否	int	校区
            round_id: that.data.round_id,
            start_date: that.data.datestart, //	否	int	开始时间
            end_date: that.data.dateend, //	否	int	结束时间
            page: that.data.page, //	否	int	页数（默认0）
            sale_state: that.data.statusid,
            lately: 0, //0-全部 1-即将开班班级
            timeSort:'',
        }
        if(Number(that.data.is_class)==2){
            obj.timeSort = that.data.pxid;
            obj['consultant_id'] = app.globalData.admin_id;
        }else{
            obj.lately = that.data.pxid;
        }
        share.wxRequest('crmapi/User/classListNew', obj, function(res) {
            if (Number(res.data.errcode) == 0) {
                var data = res.data.data;
                for (var i = 0; i < data.length; i++) {
                    data[i]['ispush'] = false;
                    data[i]['morelist'] = [];
                }
                if (Number(that.data.page) > 0) {
                    var list = that.data.arrlist.concat(data);
                } else {
                    var list = data
                }
                that.setData({
                    arrlist: list
                })
            }
            that.setData({
                shufalse: true,
            })
            wx.hideLoading();
        })
    },

    consStuList(){
        var that = this;
        wx.showLoading({
            title: '加载中~',
        })
        var objdata = {
            phone:that.data.keyword,
        }
        share.wxRequest('crmapi/user/findUserClass', objdata, function(res) {
            if (Number(res.data.errcode) == 0) {
                var data = res.data.data;
                if (Number(that.data.page) > 0) {
                    var list = that.data.arrpeoplelist.concat(data);
                } else {
                    var list = data
                }
                that.setData({
                    arrpeoplelist: list
                })
            }
            that.setData({
                shufalse: true,
            })
            wx.hideLoading();
        })
    },


    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        var that = this;
        var page = that.data.page;
        that.setData({
            page: Number(page) + 1
        })
        that.listpeople();
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})